package com.oafile.oafile.controller;


import com.alibaba.fastjson.JSONObject;
import com.oafile.oafile.mapper.ContractMapper;
import com.oafile.oafile.model.Contract;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ContractController {
	@Autowired
	private ContractMapper contractMapper;
	//public static final String FILE_BOUNDARY = "-----";
	@GetMapping("/handContractFile")
	public List<Contract> handContractFile(){
		List<Contract> contracts= contractMapper.selectContraclList();

		for (Contract contract:contracts) {
			String fileId="";
			if(StringUtils.isEmpty(contract.getFileId())){
				fileId="1";
			}else{
				fileId=contract.getFileId();
			}

			File uploadFile=new File("D:/test.doc");
			Long fileUrl=fileUpload(uploadFile);
			Map<String,Object> map=new HashMap<>();
			map.put("attRefeRence",contract.getId());
			map.put("subRefence",fileId);
			map.put("fileName",uploadFile.getName());
			map.put("fileUrl",fileUrl);
			map.put("fileSize",uploadFile.length());
			contractMapper.insertFile(map);
		}


		return contracts;
	}



	public Long fileUpload(File uploadFile){


		String tokenid = "bb7e44b2-0ae8-4fb0-83a3-b5e60afb4760";
		try {
			URL preUrl = new URL("http://toa.365power.cn/seeyon/rest/attachment?token="+tokenid+"&applicationCategory=0&extensions=&firstSave=true");
			// 设置请求�?
			HttpURLConnection hc = (HttpURLConnection)preUrl.openConnection();
			hc.setDoOutput(true);
			hc.setUseCaches(false);
			hc.setRequestProperty("contentType", "charset=utf-8");
			hc.setRequestMethod("POST");
			hc.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + "-----");
			//
			DataOutputStream dos = new DataOutputStream(hc.getOutputStream());
			dos.write(getStartData(uploadFile));
			BufferedInputStream input = new BufferedInputStream(new FileInputStream(uploadFile));
			int data = 0;
			while((data = input.read()) != -1) {
				dos.write(data);
			}
			dos.write(("\r\n--" + "-----" + "--\r\n").getBytes());
			dos.flush();
			dos.close();
			StringBuffer sb = new StringBuffer();
			InputStream is = hc.getInputStream();
			int ch;
			while((ch = is.read()) != -1) {
				sb.append((char)ch);
			}
			if(is != null)
				is.close();
			if(input != null)
				input.close();
			if(sb.toString() != null && !"".equals(sb.toString())) {
				System.out.println("附件上传成功！！ID:" + sb.toString());
			} else {
				System.out.println("附件上传失败！！");
			}

			JSONObject json= JSONObject.parseObject(sb.toString());
			String atts=json.get("atts").toString();
			Long fileId=Long.valueOf(json.getJSONArray("atts").getJSONObject(0).get("fileUrl").toString());
			System.out.println(fileId);
			return fileId;
		}catch(Exception e) {
			System.out.println("附件上传失败！！错误信息�?" + e.getMessage());
			e.printStackTrace();
		}

		return null;
	}



	private static byte[] getStartData(File file) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("--");
		sb.append("-----");
		sb.append("\r\n");
		sb.append("Content-Disposition: form-data; \r\n name=\"1\"; filename=\"" + file.getName() + "\"\r\n");
		sb.append("Content-Type: msoffice\r\n\r\n");
		return sb.toString().getBytes("UTF-8");
	}


}
